import {computed, h, ref, unref} from 'vue';
import {NAvatar, NSwitch, NTag} from 'naive-ui';
import TableItemSwitch from './tableItemSwitch.vue'
import {acceptHMRUpdate} from "pinia";

export const columns = [
  {
    title: "用户ID",
    key: "userId",
    width:80
  }, {
    title: '电话',
    key: 'phone',
    width: 150
  }, {
    title: '角色昵称',
    key: 'gameNickName',
  },
  {
    title: '渠道昵称',
    key: 'chanName',
    width: 200
  }, {
    title: '微信绑定',
    key: 'weChatBind',
    render(row) {
    return row.weChatBind?row.weChatBind:"暂无"
    }
  }, {
    title: '注册游戏',
    key: 'gameName'
  }, {
    title: '身份证',
    key: 'cardCode',
    render(row) {
      return row.cardCode?row.cardCode:"暂无"
    }
  }, {
    title: '真实姓名',
    key: 'cardName',
    render(row) {
      return row.cardName?row.cardName:"暂无"
    }
  }, {
    title: "注册时间",
    key: 'registTime'
  }, {
    title: "用户状态",
    fixed: 'right',
    width:100,
    key: 'userStatus',
    render(row) {
      return h(TableItemSwitch, {
        itemValue: row.userStatus === 'able' ? '启用' : '禁用',
        switchStatus: row.userStatus === 'able',
        itemKey: row.userId,
        onUpload: actionFun("userStatusSwitch")
      })
    }
  },
  {
    title: '注册IP',
    width:150,
    key: 'ip',
    fixed: 'right',
    render(row) {
      return h(TableItemSwitch, {
        itemValue: row.ip,
        switchStatus: row.ipStatus === 'able',
        itemKey: row.userId,
        onUpload: actionFun("ipStatusSwitch")
      })
    }
  }, {
    title: '设备ID',
    width:150,
    key: 'deviceId',
    fixed: 'right',
    render(row) {
      return h(TableItemSwitch, {
        itemValue: row.deviceId,
        switchStatus: row.deviceStatus === 'able',
        itemKey: row.userId,
        onUpload: actionFun("deviceStatusSwitch")
      })
    }
  }
];

const switchAction = ref<StatusSwitchAction[]>([]);

export interface StatusSwitchAction {
  type: string
  action: Function
}

function actionFun(type: string) {
  for (let action of unref(switchAction)) {
    if (type === action.type) {
      return action.action;
    }
  }
  return () => {
  }
}

export function registerHandler(actions: StatusSwitchAction[]) {
  switchAction.value = actions
}

